﻿using System;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Diagnostics;


internal class Program
{
    private static void Main(string[] args)
    {
        Application wordApplication = new()
        {
            // Visible = true
            DisplayAlerts = WdAlertLevel.wdAlertsNone,
            DisplayRecentFiles = false,
            DisplayDocumentInformationPanel = false,
            FeatureInstall = Microsoft.Office.Core.MsoFeatureInstall.msoFeatureInstallNone,
        };

        AppDomain.CurrentDomain.ProcessExit += (s, e) =>
        {
            wordApplication.Quit();
        };
        Console.CancelKeyPress += (s, e) =>
        {
            wordApplication.Quit();
        };

        while (true)
        {
            Console.WriteLine("请输入一个文件路径(docx)和目标文件路径(docx/pdf)（以空格分隔）：");
            string? input = Console.ReadLine();

            if (input == null)
            {
                continue;
            }

            if (input.Equals("quit", StringComparison.CurrentCultureIgnoreCase))
            {
                break;
            }

            string[] paths = input.Split(' ');

            if (paths.Length == 2)
            {
                string docxFilePath = ToAbsolutePath(paths[0]);
                string dstFilePath = ToAbsolutePath(paths[1]);

                if (Directory.Exists(dstFilePath))
                {
                    dstFilePath = dstFilePath + "/" + Path.GetFileNameWithoutExtension(docxFilePath) + ".pdf";
                }

                if (File.Exists(docxFilePath))
                {
                    Stopwatch stopwatch = new();
                    stopwatch.Start();
                    if (File.Exists(dstFilePath))
                    {
                        File.Delete(dstFilePath);
                    }
                    Document wordDocument = wordApplication.Documents.Open(docxFilePath, false, true, false);

                    if (dstFilePath.EndsWith("pdf"))
                    {
                        wordDocument.SaveAs2(dstFilePath, WdSaveFormat.wdFormatPDF);
                        Console.WriteLine("文件转换成功！\n运行时间: {0} 毫秒", stopwatch.ElapsedMilliseconds);
                    }
                    else if (dstFilePath.EndsWith("docx"))
                    {
                        wordDocument.SaveAs2(dstFilePath, WdSaveFormat.wdFormatDocumentDefault);
                        Console.WriteLine("文件转换成功！\n运行时间: {0} 毫秒", stopwatch.ElapsedMilliseconds);
                    }
                    else
                    {
                        Console.WriteLine("文件类型不支持！");
                    }
                    wordDocument.Close();
                    stopwatch.Stop();
                }
                else
                {
                    Console.WriteLine("源文件不存在！");
                }
            }
            else
            {
                Console.WriteLine("格式错误，请重新输入！");
            }

        }
    }

    private static string ToAbsolutePath(string path)
    {
        if (Path.IsPathRooted(path))
        {
            return path;
        }
        string currentPath = Directory.GetCurrentDirectory();
        string absolutePath = Path.Combine(currentPath, path);
        return absolutePath;
    }
}